﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FVideoDetails : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FVideoDetails";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("FTitle", "Chi tiết Video clip");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("DefaultView",GetText("DefaultView","Ngầm định (Chuẩn)"),1)
                };
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FVideoDetails";
            }
        }

        public string GetViewVideo(string belongSite,string ShortId)
        {
            string Url = Environments.FrontHttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/vab-" + ShortId + "." + Environments.Extension;
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }       

        public string GetVideoDetails(string belongSite, string Subject, string ShortId)
        {
            Subject = Environments.ConvertSubject(Subject);
            string Url = Environments.FrontHttpRoot + "/" + belongSite + "/" + Subject + "-video-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;

            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }
            
        private string ClientRenderWebPart(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception(GetText("TabModuleIdNull", "TabModuleId is null! Csdl sai"));
            string VideoId = (string)Environments.Request("VideoId");
            if (VideoId == null) VideoId = "";
            if (VideoId.Trim().Length == 0)
            {
                return "<div class=\"clrfix\"></div>" +
                       "<div class=cssAccessDenided>" + GetText("AccessDenided", "Yêu cầu truy cập không hợp lệ!") + "</div>";
            }
            DataTable dtItems = new VideoBLL().ReadingVideoItems(VideoId);
            string RetHtml = "";
            if (dtItems.Rows.Count == 0)
            {
                return "<div class=\"clrfix\"></div>"+
                       "<div class=cssNoImageForVideo>" + GetText("NoImage", "Không có video clip nào trong bộ sưu tập ")+"</div>";
            }

            int iIndex = 0;

            string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
            string AdvImageId = (string)dtItems.Rows[iIndex]["fk_AdvImage_Id"];
            string belongSite = (string)dtItems.Rows[iIndex]["belongSite"];
            string Subject = "";
            try
            {
                Subject = (string)dtItems.Rows[iIndex]["AdvImageItemName"];
            }
            catch { }
            string Description = "";
            try
            {
                Description = (string)dtItems.Rows[iIndex]["Description"];
            }
            catch { }
            string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
            string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
            string VideoUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;

            string EntryDate = ((DateTime)dtItems.Rows[iIndex]["ItemEntryDate"]).ToString("dd-MM-yyyy");

            RetHtml +=
                 "<div class=\"PageTitleBar\">\n" +
                        "				<ul class=\"breadcrumb\">\n" +
                        "					<li class=\"breadcrumblv1\"><a href=\"VideoList.aspx\">Thư viện Video</a></li>\n" +
                        "					<li class=\"breadcrumblv2\"><a href=\"#\">Xem chi tiết</a></li>\n" +
                        "				</ul>\n" +
                        "				<div class=\"clr\"></div>\n" +
                "</div>\n" +
                "<div class=\"ArticleDetail\">\n" +
                "			<h3 class=\"ArticleTittle\">" + Subject + "</h3>\n" +
                "				<p class=\"date\">Ngày " + EntryDate + "</p>\n" +
                "				<p class=\"desc\">" + Description + "</p>\n";
            RetHtml +=
                "<div class=\"cssVideo_details\">" +
                "       <a href=\"" + VideoUrl + "\"  " +
                "           style=\"color:#295c72; display:block;width:550px;height:440px\"  " +
                "           id=\"player" + AdvImageItemId + "\"> " +
                "       </a> 	" +
                //"<div style=\"text-align:center;color:white;\">" + advImageName + "</div>" +
                "<!-- this will install flowplayer inside previous A- tag. -->" +
                "<script>" +
                "    flowplayer(\"player" + AdvImageItemId + "\", \"flowplayer/flowplayer-3.1.2.swf\", \r\n" +
                "    { \r\n" +
                "        clip: { \r\n" +
                "			 " +
                "	        // these two configuration variables does the trick \r\n" +
                "	        autoPlay: true,  \r\n" +
                "	        autoBuffering: true // <- do not place a comma here   \r\n" +
                "       } \r\n" +
                "    });\r\n" +
                "</script>"+
                "</div>";
            DataTable dtRelation = new VideoBLL().ReadingRelation(VideoId, AdvImageId);
            RetHtml += "<div class=related>" +
                             "<h3>" + GetText("OtherVideoClip", "Video clip liên quan") + "</h3>" +
                                 "<ul>";
            for (int jIndex = 0; jIndex < dtRelation.Rows.Count; jIndex++)
            {
                AdvImageItemId = (string)dtRelation.Rows[jIndex]["AdvImageItemId"];
                AdvImageId = (string)dtRelation.Rows[jIndex]["fk_AdvImage_Id"];
                belongSite = (string)dtRelation.Rows[jIndex]["belongSite"];
                Subject = "";
                try
                {
                    Subject = (string)dtRelation.Rows[jIndex]["AdvImageItemName"];
                }
                catch { }
                Description = "";
                try
                {
                    Description = (string)dtRelation.Rows[jIndex]["Description"];
                }
                catch { }
                FileImage = (string)dtRelation.Rows[jIndex]["FileImage"];
                string Url = GetVideoDetails(belongSite, Subject, AdvImageItemId.Substring(0, 8));

                RetHtml += "<li><a href=\"" + Url + "\">" + Subject + "</a></li>";
                if (jIndex == 9) break;
            }
            RetHtml += "</ul>";
            RetHtml += "</div>" +
                    "</div>";
            return RetHtml;
           
        }


        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("DefaultView"))
                {
                    return ClientRenderWebPart(TabModuleId, HasPorlet);
                }
                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

      
        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("Id");
            dtResult.Columns.Add("Subject");
            dtResult.Rows.Add(new object[]{
                "69EB07FC-EA01-46fe-A1B3-5BDA611C7978","Ngầm định."
            });
            return dtResult;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }
    }
}
